package com.qiankan.qmsj.utils;

import android.support.annotation.NonNull;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2017/7/24.
 */

public class UploadUtils {
    public static final String MULTIPART_FORM_DATA = "multipart/form-data";
    public static final String MULTIPART_FORM_BODY = "text/plain";
    public static final String APPLICATION_ZIP = "application/zip";

    @NonNull
    public static RequestBody createPartFromString(String descriptionString) {
        return RequestBody.create(
                MediaType.parse(APPLICATION_ZIP), descriptionString);
    }

    @NonNull
    public static RequestBody createBodyFromString(String descriptionString) {
        return RequestBody.create(
                MediaType.parse(MULTIPART_FORM_BODY), descriptionString);
    }

    @NonNull
    public static MultipartBody.Part prepareFilePart(String partName, String file) {

        // 为file建立RequestBody实例
        RequestBody requestFile = RequestBody.create(MediaType.parse(MULTIPART_FORM_DATA), new File(file));

        // MultipartBody.Part借助文件名完成最终的上传
        return MultipartBody.Part.createFormData(partName, new File(file).getName(), requestFile);
    }
}
